package com.ly.cloud.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.ApprovalDetailDto;
import com.ly.cloud.entity.ApprovalDetailPo;
import com.ly.cloud.exception.mapper.DBException;
import com.ly.cloud.mapper.ApprovalDetailMapper;
import com.ly.cloud.mapper.ApprovalMapper;
import com.ly.cloud.service.ApprovalService;
import com.ly.cloud.vo.ApprovalCategoryVo;
import com.ly.cloud.vo.ApprovalDetailVo;
import com.ly.cloud.vo.RoleVo;

/**
 * Class Name: ApprovalServiceImpl Description: 审批流程服务实现类
 * 
 * @author: siqi
 * @mail: siqi@ly-sky.com
 * @date: 2018年12月7日
 * @version: 1.0
 *
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public class ApprovalServiceImpl implements ApprovalService {

	private static final Long SFQY_IS = 1L;// 是否启用 是
	private static final Long SFQY_NOT = 0L;// 是否启用 否

	@Autowired
	public ApprovalMapper approvalMapper;

	@Autowired
	public ApprovalDetailMapper approvalDetailMapper;

	/**
	 *  Title: queryPageAprvCategory Description: 分页查询审批流程 Created On:
	 * 2018年12月10日
	 * 
	 * @author SiQi
	 *         <p>
	 * @param page
	 * @param name
	 * @return ApprovalServiceImpl
	 * @see com.ly.cloud.service.ApprovalService#queryPageAprvCategory(com.baomidou.mybatisplus.plugins.Page)
	 */
	@Override
	public Page<ApprovalCategoryVo> queryPageAprvCategory(Page<ApprovalCategoryVo> page, String name) {
		try {
			return page.setRecords(approvalMapper.queryAllApprovalCategory(page, name));
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败！", e);
		}
	}

	/**
	 *  Title: queryPageAprvCategory Description: 分页查询审批流程步骤 Created
	 * On: 2018年12月10日
	 * 
	 * @author SiQi
	 * @param page
	 * @param name
	 * @return ApprovalServiceImpl
	 * @see com.ly.cloud.service.ApprovalService#queryPageAprvCategory(com.baomidou.mybatisplus.plugins.Page)
	 */
	@Override
	public List<ApprovalDetailVo> queryApprovalDetailByCategoryID(Long categoryID) {
		try {
			List<ApprovalDetailVo> detailList = null;
			detailList = approvalDetailMapper.queryApprovalDetailByCategoryID(categoryID);
			return detailList;
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败！", e);
		}
	}

	/**
	 * Title: queryPageAprvCategory Description: 设置是否启用审批流程 On:
	 * 2018年12月10日
	 * 
	 * @author SiQi
	 * @param page
	 * @param name
	 * @return boolean)
	 */
	@Override
	public boolean updateCategoryIsEnable(Long is_enable, Long categoryID) {
		try {
			if (is_enable != null && categoryID != null) {
				HashMap<String, Long> map = new HashMap<String, Long>();
				map.put("is_enable", is_enable);
				map.put("categoryID", categoryID);
				int rs = approvalMapper.updateCategoryIsEnable(map);
				if (rs > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("更新失败！", e);
		}
	}

	/**
	 * Title: insertApprovalDetail Description: On: 2018年12月10日
	 * 新增审批流程步骤
	 * 
	 * @author SiQi
	 * @param dto
	 * @return ApprovalServiceImpl
	 */
	@Override
	public boolean insertApprovalDetail(String username,ApprovalDetailDto approvalDetaildto) {

		ApprovalDetailPo approvalDetailPo = new ApprovalDetailPo();

		BeanUtils.copyProperties(approvalDetaildto, approvalDetailPo);
		
		approvalDetailPo.setCjr(username);
		approvalDetailPo.setCjsj(new Date());
		approvalDetailPo.setSfqy(SFQY_IS); // 新增时默认是启用
		Integer i;
		try {
			i = approvalDetailMapper.insert(approvalDetailPo);
			if (i > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("插入失败！", e);
		}
	}

	/*
	 * Title: queryPageAprvCategory Description: 设置是否启用审批步骤
	 * 2018年12月10日
	 * 
	 * @author SiQi
	 * 
	 * @param is_enable
	 * 
	 * @param detailID
	 * 
	 * @return boolean)
	 */
	@Override
	public boolean updateDetailIsEnable(Long is_enable, Long detailID) {
		try {
			if (is_enable != null && detailID != null) {
				HashMap<String, Long> map = new HashMap<String, Long>();
				map.put("is_enable", is_enable);
				map.put("detailID", detailID);
				int rs = approvalDetailMapper.updateDetailIsEnable(map);
				if (rs > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new DBException("更新失败！", e);
		}
	}
	
	/**
	 * 查询角色列表
	 */
	@Override
	public List queryRolesList() {
		try {
			List<RoleVo> roleList = new ArrayList<RoleVo>();
			
			/*
			 * 测试数据 ，后面需要调用其他服务获取角色列表
			 */
			RoleVo vo1 = new RoleVo();
			vo1.setJcbh("gly");
			vo1.setJsmc("管理员");
			
			RoleVo vo2 = new RoleVo();
			vo2.setJcbh("ejxyld");
			vo2.setJsmc("二级学院领导");
			
			roleList.add(vo1);
			roleList.add(vo2);
			
			return roleList;
		}catch(Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败！", e);
		}
	}
	
	/**
	 * Title: updateDetailById Description: On: 2018年12月13日
	 * 修改审批流程步骤
	 * @author SiQi
	 * @param dto
	 * @return ApprovalServiceImpl
	 */
	public boolean updateDetailById(@Param("loginUserName")String userName,@Param("approvaldatail")ApprovalDetailDto dto) {
		try {
			ApprovalDetailPo approvalDetailPo = new ApprovalDetailPo();

			BeanUtils.copyProperties(dto, approvalDetailPo);

			approvalDetailPo.setXgsj(new Date());
			approvalDetailPo.setXgr(userName);
			
			Integer i = approvalDetailMapper.updateById(approvalDetailPo);
			if (i > 0) {
				return true;
			} else {
				return false;
			}
		}catch(Exception e) {
			e.printStackTrace();
			throw new DBException("查询失败！", e);
		}
	}

	/**
	 * 审批初始化，提交业务时调用
	 * 返回的信息(0:成功,1:未定义流程,2:不启用审批流程,直接通过)
	 */
	@Override
	public String initApproval(String ywzj, String tableName) {
		try {
			Map<String, String> param = new HashMap<String, String>();
			param.put("ywzj", ywzj);
			param.put("tableName", tableName);
			approvalDetailMapper.initApproval(param);
			String msg = param.get("result");// 返回的信息(0:成功,1:未定义流程,2:不启用审批流程,直接通过)
			return msg;
		} catch (Exception e) {
			throw new DBException("审批初始化出错，请联系管理员", e);
		}
	}

	/**
	 * 审批处理过程
	 * 返回的审批状态信息(8:审批不通过,9审批通过)
	 */
	@Override
	public String processApproval(String ywzj, String tableName, String sprbh, String spyj, String spzt) {
		try {
			Map<String, String> param = new HashMap<String, String>();
			param.put("ywzj", ywzj);
			param.put("tableName", tableName);
			param.put("sprbh", sprbh);
			param.put("spyj", spyj);
			param.put("spzt", spzt);
			approvalDetailMapper.processApproval(param);
			String msg = param.get("result");// 返回的审批状态信息(8:审批不通过,9审批通过)
			return msg;
		} catch (Exception e) {
			throw new DBException("审批过程出错，请联系管理员", e);
		}
	}
}
